package cn.growthgate.fgo.web;

import java.util.Arrays;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.growthgate.fgo.common.Constants;
import cn.growthgate.fgo.entity.Image;
import cn.growthgate.fgo.entity.Item;
import cn.growthgate.fgo.service.IImageService;
import cn.growthgate.fgo.service.IItemService;
import cn.growthgate.fgo.util.StringUtils;
import cn.growthgate.fgo.web.result.JsonResult;
import cn.growthgate.fgo.web.result.TableResult;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author GrowthGate
 * @since 2019-01-20
 */
@Controller
@RequestMapping("/item")
public class ItemController {

	@Autowired
	private IItemService itemService;

	@Autowired
	private IImageService imageService;

	@RequestMapping("/list")
	@RequiresRoles(value = {Constants.ROLE_ADMIN}, logical = Logical.OR)
	public ModelAndView list() {
		return new ModelAndView("item/list");
	}

	@ResponseBody
	@RequestMapping("/tableData")
	public TableResult<Item> tableData(Item item, Integer current, Integer size, String orderByField, Boolean isAsc) {
		Wrapper<Item> wrapper = new EntityWrapper<>();

		if (item.getType() != null) {
			wrapper.eq("type", item.getType());
		}
		if (item.getRare() != null) {
			wrapper.eq("rare", item.getRare());
		}
		if (StringUtils.isNotBlank(item.getName())) {
			wrapper.andNew().like("name", item.getName()).or().like("jName", item.getName()).or()
					.like("eName", item.getName()).or().like("nickName", item.getName());
		}

		Page<Item> page = itemService.selectPage(current, size, orderByField, isAsc, wrapper);
		return new TableResult<Item>(page);
	}

	@RequestMapping("/form")
	@RequiresRoles(value = {Constants.ROLE_ADMIN}, logical = Logical.OR)
	public ModelAndView form(Long id) {
		Item entity = itemService.selectById(id);

		return new ModelAndView("item/form").addObject("entity", entity);
	}

	@ResponseBody
	@RequestMapping("/save")
	@RequiresRoles(value = {Constants.ROLE_ADMIN}, logical = Logical.OR)
	public JsonResult save(@Validated Item item, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return JsonResult.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
		}
		if (item.getId() != null) {
			// 编辑
			Item oItem = itemService.selectById(item.getId());
			// 更改图片
			if (oItem.getImage() != null && !oItem.getImage().equals(item.getImage())) {
				imageService.deleteByUUID(oItem.getImage());
			}
		}
		itemService.insertOrUpdate(item);
		if (StringUtils.isNotBlank(item.getImage())) {
			Image image = imageService.getByUUID(item.getImage());
			image.setEntityId(item.getId());
			image.setEntityName(item.getClass().getSimpleName());
			imageService.updateById(image);
		}
		return JsonResult.ok();
	}

	@ResponseBody
	@RequestMapping("/delete")
	@RequiresRoles(value = {Constants.ROLE_ADMIN}, logical = Logical.OR)
	public JsonResult delete(Long[] ids) {
		if (itemService.deleteBatchIds(Arrays.asList(ids))) {
			return JsonResult.ok("删除成功");
		} else {
			return JsonResult.fail("删除失败");
		}
	}

	@RequestMapping("/show")
	public ModelAndView show() {
		return new ModelAndView("item/show");
	}

}
